<?php
namespace Mapi\Controller;
use Think\Controller\RestController;

class BaseController extends RestController {    

    protected $uid;
    protected $allowMethod    = array('get','post','put'); // REST允许的请求类型列表
    protected $allowType      = array('html','xml','json'); // REST允许请求的资源类型列表

    public function __construct() {
    	parent::__construct();

        if(!in_array(ACTION_NAME,array('getStreamUrl','qiniuCallBack','setQiniuVideoThumb','vframeQiniu'))) {
            $token = I('server.HTTP_USERTOKEN');


            if(!$token) {
                $message['resultCode'] = 2;
                $message['message'] = '用户未登录';           
                exit(json_encode($message));            
            } else {
                $loginInfo = M('UserLoginInfo')->where(array('token' => $token))->find();

                if($loginInfo) {
                    if($loginInfo['valid_status'] == 2) {
                        $message['resultCode'] = 2;
                        $message['message'] = '您的账号已在别处登录';
                        exit(json_encode($message));          
                    } elseif($loginInfo['valid_status'] == 0) {
                        $message['resultCode'] = 2;
                        $message['message'] = '您的账号已退出登录';
                        exit(json_encode($message));                   
                    }
                } else {
                    $message['resultCode'] = 2;
                    $message['message'] = '用户不存在';
                    exit(json_encode($message));                
                }
                

                $sys_version = I('server.HTTP_SYSTEMVERSION');
                $client_type = I('server.HTTP_CLIENTTYPE');
                $device_name = I('server.HTTP_DEVICENAME');
                $app_version = I('server.HTTP_APPVERSION');
                $update_time = time();

                $client_type && $dataGet['clienttype'] = $client_type; 
                $device_name && $dataGet['devicename'] = $device_name; 
                $app_version && $dataGet['appversion'] = $app_version; 
                $sys_version && $dataGet['system_version'] = $sys_version; 
                $dataGet['update_time'] = time();

                M('UserLoginInfo')->where(array('token' => $token))->setInc('req_num', 1);
                M('UserLoginInfo')->where(array('token' => $token))->save($dataGet);

                $this->uid = $loginInfo['reporter_id'];
                // $this->logininfo = $loginInfo;
                // $this->token = $token;
            }


/*            if(!$token) {
                $message['resultCode'] = 2;
                $message['message'] = '用户未登录';           
                $this->ajaxReturn($message);            
            } else {
                $loginInfo = M('UserLoginInfo')->where(array('token' => $token))->find();
                if(empty($loginInfo)) {
                    $message['resultCode'] = 2;
                    $message['message'] = '您的账号已在别处登录';            
                    $this->ajaxReturn($message);                
                }


                $sys_version = I('server.HTTP_SYSTEMVERSION');
                $client_type = I('server.HTTP_CLIENTTYPE');
                $device_name = I('server.HTTP_DEVICENAME');
                $app_version = I('server.HTTP_APPVERSION');
                $update_time = time();

                $client_type && $dataGet['clienttype'] = $client_type; 
                $device_name && $dataGet['devicename'] = $device_name; 
                $app_version && $dataGet['appversion'] = $app_version; 
                $sys_version && $dataGet['system_version'] = $sys_version; 
                $dataGet['update_time'] = time();

                M('UserLoginInfo')->where(array('token' => $token))->setInc('req_num', 1);
                M('UserLoginInfo')->where(array('token' => $token))->save($dataGet);

                $this->uid = $loginInfo['uid'];

                // $username = M('UserInfo')->where(array('uid' => $loginInfo['uid']))->getField('realname');
                // setlog($username, $this->uid, CONTROLLER_NAME, ACTION_NAME);
            }*/
        }


    }
}